import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { StorageLocal,StorageSession } from "@/storage/storage";
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Index',
    meta: {
      title: '大数据'
    },
    component: () => import('../views/index/index.vue')
  },
  {
    path: '/node',
    name: 'Node',
    meta: {
      title: '节点大数据'
    },
    component: () => import('../views/index/node.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆

router.beforeEach((to, from, next) => {
  if (to.path === "/") {
    next();
  } else {
    const token = StorageSession.get('token')
    console.log(token)
    if(token){
      // 有 token
      if (token === undefined || token === null || token === "") {
        next("/");
      } else {
        next();
      }
    } else {
      next("/");
    }

  }
});



export default router
